using System;
using System.Collections.Generic;
using UnityEngine;

public class RankMgr
{
	private class CompareByStar : IComparer<RankInfo>
	{
		public int Compare(RankInfo x, RankInfo y)
		{
			return y.star - x.star;
		}
	}

	private class CompareByScore : IComparer<RankInfo>
	{
		public int Compare(RankInfo x, RankInfo y)
		{
			return y.score - x.score;
		}
	}

	private static List<RankInfo> _rankInfos = new List<RankInfo>();

	private static RankInfo _playerRankInfo;

	public static int _type = 0;

	private static long _firstRefreshTime = 0L;

	private static long _lastRefreshTime = 0L;

	private static string FIRST_REFRESH_TIME_KEY = "firstRefreshTime";

	private static string LAST_REFRESH_TIME_KEY = "lastRefreshTime";

	public static List<RankInfo> RankInfos
	{
		get
		{
			return _rankInfos;
		}
		set
		{
			_rankInfos = value;
		}
	}

	public static RankInfo playerRankInfo
	{
		get
		{
			if (_playerRankInfo == null)
			{
				_playerRankInfo = new RankInfo();
				_playerRankInfo.name = "你";
			}
			_playerRankInfo.star = ConstantData.missionRecords.GetTotalStar();
			_playerRankInfo.score = ConstantData.missionRecords.GetTotalScore();
			return _playerRankInfo;
		}
	}

	public static int playerRank => GetRankInfos(_type).IndexOf(playerRankInfo);

	public static List<RankInfo> GetRankInfos(int type)
	{
		_rankInfos.Clear();
		double spanFromFirstRefresh = GetSpanFromFirstRefresh();
		int rankArrayIndex = GetRankArrayIndex();
		rankArrayIndex = Mathf.Min(rankArrayIndex, RankConfig.rankInfos.Count / 20 - 1);
		_rankInfos.AddRange(RankConfig.rankInfos.GetRange(rankArrayIndex * 20, 20));
		if (GetSpanFromLastRefresh() == 0.0 || GetSpanFromLastRefresh() > (double)CommonConfig.CommonCfgInfo.rankRefreshInterval)
		{
			for (int i = 0; i < _rankInfos.Count; i++)
			{
				_rankInfos[i].name = "玩家 " + UnityEngine.Random.Range(1000, 100000);
			}
			_lastRefreshTime = DateTime.Now.ToFileTime();
		}
		_rankInfos.Add(playerRankInfo);
		SortRankInfos(type);
		if (_firstRefreshTime == 0)
		{
			_firstRefreshTime = DateTime.Now.ToFileTime();
		}
		return _rankInfos;
	}

	private static int GetRankArrayIndex()
	{
		int totalStar = ConstantData.missionRecords.GetTotalStar();
		int totalScore = ConstantData.missionRecords.GetTotalScore();
		for (int num = CommonConfig.rankStages.Count - 1; num >= 0; num--)
		{
			RankStage rankStage = CommonConfig.rankStages[num];
			if (totalStar >= rankStage.star && totalScore >= rankStage.score)
			{
				return num + 1;
			}
		}
		return 0;
	}

	public static void SortRankInfos(int type)
	{
		_type = type;
		switch (type)
		{
			case 0:
				_rankInfos.Sort(new CompareByStar());
				break;
			case 1:
				_rankInfos.Sort(new CompareByScore());
				break;
		}
	}

	private static double GetSpanFromFirstRefresh()
	{
		if (_firstRefreshTime == 0)
		{
			return 0.0;
		}
		DateTime d = DateTime.FromFileTime(_firstRefreshTime);
		return (DateTime.Now - d).TotalSeconds;
	}

	private static double GetSpanFromLastRefresh()
	{
		if (_lastRefreshTime == 0)
		{
			return 0.0;
		}
		DateTime d = DateTime.FromFileTime(_lastRefreshTime);
		return (DateTime.Now - d).TotalSeconds;
	}

	public static void Load()
	{
		if (PlayerPrefs.HasKey(FIRST_REFRESH_TIME_KEY))
		{
			_firstRefreshTime = (long)PlayerPrefs.GetFloat(FIRST_REFRESH_TIME_KEY);
		}
		if (!PlayerPrefs.HasKey(LAST_REFRESH_TIME_KEY))
		{
		}
	}

	public static void Save()
	{
		PlayerPrefs.SetFloat(FIRST_REFRESH_TIME_KEY, _firstRefreshTime);
		PlayerPrefs.SetFloat(LAST_REFRESH_TIME_KEY, _lastRefreshTime);
	}
}
